use "./raw data/indiv2.dta", clear
merge 1:1 ident using "./raw data/polrel2.dta"
drop _merge

// Controls and parents' characteristics
ren regionnaise2 bpl

ren regionnaisp2 fbpl
destring fbpl, replace
replace fbpl=. if fbpl==9999

ren regionnaism2 mbpl
destring mbpl, replace
replace mbpl=. if mbpl==9999

ren anaise birthyear

gen female=(sexee=="2")

gen muslimcountry=(fbpl==2101|fbpl==2201|fbpl==2301|fbpl==3401|fbpl==2401|fbpl==2402|fbpl==2403 ///
					|fbpl==2405|fbpl==2406|fbpl==2407|fbpl==2408|fbpl==2409|fbpl==5106)
replace muslimcountry=. if fbpl==9999

egen religion=group(relego1)
replace religion=. if relego1=="88"|relego1=="99"

gen muslim=(relego1=="20"|relego1=="21")
replace muslim=. if relego>="88"

foreach p in f m {
	gen algeria`p'=(`p'bpl==2101)
	gen morocco`p'=(`p'bpl==2201)
	gen tunisia`p'=(`p'bpl==2301)
	gen turkey`p'=(`p'bpl==3401)
	gen senegal`p'=(`p'bpl==2401)
	gen mauritania`p'=(`p'bpl==2402)
	gen gambia`p'=(`p'bpl==2403)
	gen guinea`p'=(`p'bpl==2405)
	gen mali`p'=(`p'bpl==2406)
	gen burkfaso`p'=(`p'bpl==2407)
	gen niger`p'=(`p'bpl==2408)
	gen chad`p'=(`p'bpl==2409)
	gen middleeast`p'=(`p'bpl==5106)
}

foreach c in algeria morocco tunisia turkey senegal mauritania gambia guinea mali burkfaso niger chad middleeast {
	foreach p in f m {
		replace `c'`p'=. if `p'bpl==.|`p'bpl==9999
	}
}

gen roafricaf=(senegalf==1|mauritaniaf==1|gambiaf==1|guineaf==1|malif==1|burkfasof==1|nigerf==1|chadf==1)
replace roafricaf=. if senegalf==.&mauritaniaf==.&gambiaf==.&guineaf==.&malif==.&burkfasof==.&nigerf==.&chadf==.

gen frenchm=(mbpl==1101)
replace frenchm=. if mbpl==.|mbpl==9999

destring relmere1, replace
destring relpere1, replace
gen muslimm=(relmere1==20|relmere1==21)
replace muslimm=. if relmere1>43
gen muslimf=(relpere1==20|relpere1==21)
replace muslimf=. if relpere1>43

gen frenchathome=(ilang==0)

destring n_frcmt, replace
gen frenchbirth=(n_frcmt==1)
replace frenchbirth=. if n_frcmt==.|n_frcmt==9

* Categorie socioprofesionelle father mother
ren isco_mere misco
ren isco_pere fisco
foreach p in m f {
	gen military`p'=(`p'isco=="01"|`p'isco=="12"|`p'isco=="13")
	gen science`p'=(`p'isco=="21"|`p'isco=="22"|`p'isco=="23"|`p'isco=="24")
	gen interm`p'=(`p'isco=="31"|`p'isco=="32"|`p'isco=="33"|`p'isco=="34")
	gen admin`p'=(`p'isco=="41"|`p'isco=="42")
	gen sales`p'=(`p'isco=="51"|`p'isco=="52")
	gen agric`p'=(`p'isco=="61")
	gen crafts`p'=(`p'isco=="71"|`p'isco=="72"|`p'isco=="73"|`p'isco=="74")
	gen oper`p'=(`p'isco=="81"|`p'isco=="82"|`p'isco=="83")
	gen unskill`p'=(`p'isco=="91"|`p'isco=="92"|`p'isco=="93")
}

foreach prof in military science interm admin sales agric crafts oper unskill {
	foreach p in f m {
		replace `prof'`p'=. if `p'isco==""|`p'isco=="00"
	}
}

* Highest degree of father and mother
ren diplom mdiplo
ren diplop fdiplo
destring mdiplo, replace
destring fdiplo, replace
foreach p in m f {
	gen nodegree`p'=(`p'diplo<20)
	gen cep`p'=(`p'diplo==20)
	gen brevet`p'=(`p'diplo==30)
	gen capbep`p'=(`p'diplo>30&`p'diplo<51)
	gen bacprof`p'=(`p'diplo>=51&`p'diplo<60)
	gen bac`p'=(`p'diplo==60)
	gen bacplus2`p'=(`p'diplo>=60&`p'diplo<81)
	gen univ`p'=(`p'diplo>=81&`p'diplo<=89)
}

foreach d in nodegree cep brevet capbep bacprof bac bacplus2 univ {
	foreach p in f m {
		replace `d'`p'=. if `p'diplo==.|`p'diplo>89
	}
}

* Labor force status of parents
ren t_situm mlfp
ren t_situp flfp
foreach p in m f {
	gen employed`p'=(`p'lfp=="1")
	gen unemployed`p'=(`p'lfp=="2")
	gen retired`p'=(`p'lfp=="3")
	gen inactive`p'=(`p'lfp=="4"|`p'lfp=="5")
}

foreach s in employed unemployed retired inactive {
	foreach p in f m {
		replace `s'`p'=. if `p'lfp=="9"|`p'lfp==""
	}
}	

* Type of employment of parents
ren t_statm mstat
ren t_statp fstat
foreach p in m f {
	gen selfemp`p'=(`p'stat=="1")
	gen family`p'=(`p'stat=="2")
	gen salary`p'=(`p'stat=="3"|`p'stat=="4")
}

foreach s in selfemp family salary {
	foreach p in f m {
		replace `s'`p'=. if `p'stat=="9"|`p'stat==""
	}
}	

* Age of father and mother at R's birth
gen ageatbirthm=birthyear-t_anaism
la var ageatbirthm "Mother's age when R born"
gen ageatbirthf=birthyear-t_anaisp
la var ageatbirthf "Father's age when R born"

* Age of father and mother at arrival to France
gen ageatarrm=t_arrivm-t_anaism
la var ageatarrm "Age of mother at arrival"
gen ageatarrf=t_arrivp-t_anaisp
la var ageatarrf "Age of father at arrival"	

* Raised by both parents
ren t_elev_a raised_both
gen raised_single=(t_elev_d==1|t_elev_e==1)
replace raised_single=. if t_elev_d==.&t_elev_e==.
la var raised_single "Raised in single parent hh"
	
* Other variables
gen history=(t_hist=="1")
replace history=. if t_hist=="9"|t_hist==""
la var history "Parents recounted migration history"

gen money=(t_argent=="1")
replace money=. if t_argent=="9"|t_argent==""
la var money "Financial troubles in family before age 18"

ren t_frsoe siblings
la var siblings "# of siblings"

// parents same background
gen samepar=(fbpl==mbpl)
replace samepar=. if fbpl==.|mbpl==.

// Assimilation measures
destring lnfrea_gr, replace
gen french_fam=(lnfrea_gr==10)
replace french_fam=. if lnfrea_gr==.|lnfrea_gr==90

destring lncj1_gr, replace
gen french_sp=(lncj1_gr==10)
replace french_sp=. if lncj1_gr==.|lncj1_gr==90

destring lnenf1_gr, replace
gen french_kids=(lnenf1_gr==10)
replace french_kids=. if lnenf1_gr==.|lnenf1_gr==90

// Outcome variables
// Religion
destring r_impvie, gen(relimp)
replace relimp=. if relimp>=8
foreach x in r_ostent r_culte r_impedu r_miam {
	destring `x', replace
}
replace r_impedu=. if r_impedu>4
recode r_ostent (4=.) (8=.) (9=.) (3=0) (2=1) (1=2)
recode r_culte (8=.) (9=.) (5=0) (4=1) (3=2) (2=3) (1=4)
recode r_miam (8=.) (9=.) (4=.) (3=0) (2=1) (1=2)

// Identity
destring x_apparf, gen(feelfrench)
destring x_apparp, gen(feelfbpl)
destring x_vufri, gen(seenasfrench)
destring x_moifr, gen(homeinfrance)

foreach x in feelfrench seenasfrench homeinfrance feelfbpl {
	replace `x'=. if `x'>=8
	recode `x' (1=4) (2=3) (3=2) (4=1)
	gen `x'2=(`x'==3|`x'==4)
	replace `x'2=. if `x'==.
}

gen feelmorefor=feelfbpl-feelfrench
replace feelmorefor=. if feelfrench==.|feelfbpl==.

// School related
destring i_ecole, replace
recode i_ecole (8=.) (9=.) (1=4) (2=3) (3=2) (4=1)

destring f_dip, replace
gen secondary=(f_dip>=4)
replace secondary=. if f_dip==.

destring f_redoub, replace
gen repclass=(f_redoub==1)
replace repclass=. if f_redoub==.

gen school_relpar=(f_sectpq_d==1)
replace school_relpar=. if f_sectpq_d==.

// Discrimination
destring d_ouraci_flag, replace
foreach x in d_ouraci_a d_ouraci_b d_ouraci_c d_ouraci_d d_ouraci_e d_ouraci_f d_ouraci_g ///
	d_ouraci_h d_ouraci_i d_ouraci_j {
	replace `x'=. if d_ouraci_flag!=1
}

gen harass_school=(d_ouraci_c==1)
replace harass_school=. if d_ouraci_c==.

gen harass_work=(d_ouraci_a==1)
replace harass_work=. if d_ouraci_a==.

gen harass_university=(d_ouraci_b==1)
replace harass_university=. if d_ouraci_b==.

gen harass_store=(d_ouraci_d==1)
replace harass_store=. if d_ouraci_d==.

gen harass_street=(d_ouraci_e==1)
replace harass_street=. if d_ouraci_e==.

gen harass_transport=(d_ouraci_f==1)
replace harass_transport=. if d_ouraci_f==.

gen harass_police=(d_ouraci_g==1)
replace harass_police=. if d_ouraci_g==.

gen harass_hospital=(d_ouraci_h==1)
replace harass_hospital=. if d_ouraci_h==.

gen harass_bank=(d_ouraci_i==1)
replace harass_bank=. if d_ouraci_i==.

gen harass_pubserv=(d_ouraci_j==1)
replace harass_pubserv=. if d_ouraci_j==.

gen harass_other=(d_ouraci_k==1)
replace harass_other=. if d_ouraci_k==.

// Health
destring s_pblog, replace
gen healthprob=(s_pblog==1)
replace healthprob=. if s_pblog==9|s_pblog==.

destring s_pbfrag, replace
gen healthpreg=(s_pbfrag==1)
replace healthpreg=. if s_pbfrag==9|s_pbfrag==.

destring s_pbpriv, replace
gen healthpriv=(s_pbpriv==1)
replace healthpriv=. if s_pbpriv==9|s_pbpriv==.

destring s_pbtrav, replace
gen healthwork=(s_pbtrav==1)
replace healthwork=. if s_pbtrav==9|s_pbtrav==.

destring s_etat, gen(health)
replace health=. if health==9
recode health (1=5) (2=4) (4=2) (5=1)

gen badhealth=(health<=2)
replace badhealth=. if health==.

gen agehp=s_agepb if s_agepb<58
replace agehp=s_agepb-birthyear if s_agepb>1000

gen healthprob_school=(healthprob==1|healthpriv==1&(agehp>=6&agehp<=18))
replace healthprob_school=. if agehp==.|(healthprob==.&healthpriv==.)

// Restrict dataset
// French born or arrived to France aged less than 6
keep if (bpl=="1101"|arrivag<6) & birthyear>=1980

// Treated cohort and interaction
gen treatedcoh=(birthyear>=1986)
gen interm=muslim*treatedcoh
gen tripleint=interm*female

// Muslim-specific trend
sum birthyear
local minyear=r(min)
local maxyear=r(max)-r(min)
gen t=birthyear-`minyear'+1
gen tmuslim=t*muslim
gen tmuslimfem=tmuslim*female


save TeO, replace


